﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Newtonsoft.Json;
using ProjectN.Core.Element;
using ProjectN.Core.Element.Def;
using ProjectN.Core.Fore;
using ProjectN.Core.Instance;
using ProjectN.GUI;

namespace ProjectN
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(EditFrame.CreateEmpty());

            return;

            ProjectN.Debug.Utilities.GeneralDebugOutput.Pushln("Debugging Enabled.");
            ProjectN.Debug.Utilities.GeneralDebugOutput.PushFln("Work Dir: {0}", Environment.CurrentDirectory);
            if (File.Exists("test-instance.zip"))
            {
                ProjectN.Debug.Utilities.GeneralDebugOutput.Pushln("Test Instance Read.");
                Instance instance = Creation.ReadFile("test-instance.zip");
                Application.Run(new GUI.ShowFrame { PageInfo = instance.GetMajorInfo() });
                return;
            }


            // Test Code
            // Core.IElement[] ids = new IElement[16384];
            // short id = 0;

            int SIZE = 1024;
            var wrapper = Core.Instance.Creation.CreateEmpty(SIZE, SIZE, "TestPage", Color.White);
            DynamicPage page = wrapper.Value.MajorPage;

            var gray = new BrushInfo { ImageMode = false, Color =Color.Gray, Image = null };
            var black = new BrushInfo { ImageMode = false, Color = Color.Black, Image = null };
            var pen = new PenInfo { Brush = gray, Width = 1 };
            var Apen = new PenInfo { Brush = black, Width = 1 };
            var brush = new BrushInfo { ImageMode = false, Color = Color.Red, Image = null };
            var font = new FontInfo { FamilyName = "微软雅黑", Size = 12, Style = FontStyle.Regular };

            for (short i = 1; i * 10 <= SIZE; ++i)
            {
                if (i % 10 == 0)
                {
                    page.Elements.Add(new Line { P1 = new(0, i * 10), P2 = new(SIZE, i * 10), Pen = Apen });
                    page.Elements.Add(new Line { P1 = new(i * 10, 0), P2 = new(i * 10, SIZE), Pen = Apen });
                    page.Elements.Add(new Text { Context = (i * 10).ToString(), Position = new(0, i * 10 + 1), Font = font, Brush = brush });
                    page.Elements.Add(new Text { Context = (i * 10).ToString(), Position = new(i * 10, 1), Font = font, Brush = brush });
                }
                else
                {
                    page.Elements.Add(new Line { P1 = new(0, i * 10), P2 = new(SIZE, i * 10), Pen = pen });
                    page.Elements.Add(new Line { P1 = new(i * 10, 0), P2 = new(i * 10, SIZE), Pen = pen });
                }
            }

            var neofont = new FontInfo { FamilyName = "方正姚体", Size = 30, Style = FontStyle.Bold | FontStyle.Underline };

            page.Elements.Add(new RectedText { Context = string.Format("Item #{0}", page.Elements.Count), Position = new(114, 514), Font = neofont, Brush = brush, BorderWidth = 12 });

            var pink = new BrushInfo { ImageMode = false, Color = Color.Pink, Image = null };
            var pinkpen = new PenInfo { Brush = pink, Width = 3 };
            var lightgreen = new BrushInfo { ImageMode = false, Color = Color.LightGreen, Image = null };
            var noncolor = new BrushInfo { ImageMode = false, Color = Color.FromArgb(0), Image = null };
            var nonpen = new PenInfo { Brush = noncolor, Width = 1 };
            
            page.Elements.Add(new RoundedRectangle { Rectangle = new RectangleF(200, 200, 500, 200), Border = nonpen, RadiusX = 15, RadiusY = 15, Fill = lightgreen });
            page.Elements.Add(new BezierBatch { Pen = pinkpen, Points = [ new(100, 200), new(200, 400), new(412, 333), new(222, 666) ] });
            //page.Elements.Add(new Completion { });

#if DEBUG
            var res = page.Data.ImageCollection.StoreFile("D:/testimg.png");
            page.Elements.Add(new SimpleImage { Image = new ImageInfo { Identity = res.Hash }, Position = new(88, 88) });

            PropertyEditForm.Show([.. page.Elements], "Debug Property Form");
#endif

            page.Update();
            wrapper.Value.SaveAll();
            Application.Run(new GUI.EditFrame { PageInfo = wrapper.Value.GetMajorInfo() });
            //Application.Run(new GUI.ShowFrame { PageInfo = wrapper.Value.GetMajorInfo() }); 
        }

    }
}
